package test4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;

public class NovelDownloaderIframe extends JFrame {
    JPanel bottomPanel;//显示所有图书数据的总布局
    //显示所有图书数据的总布局
    JScrollPane scrollPane;

    public NovelDownloaderIframe() throws HeadlessException {
        init();
        this.setVisible(true);//显示
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void init() {
        this.setTitle("小说下载器");
        this.setSize(400, 600);
        this.setLocationRelativeTo(null);//居中
        this.setResizable(false);//禁止缩放
        this.setLayout(new BorderLayout());


        //顶部布局 添加搜索框和按钮
        JPanel topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(400, 50));
        topPanel.setBackground(new Color(90, 90, 200));
        //设置布局为流式布局 居中显示 左右间距30 上下间距0
        topPanel.setLayout(null);

        JTextField searchField = new JTextField();
        searchField.setSize(230, 30);
        searchField.setLocation(20, 10);
        JButton searchBtn = new JButton("搜索");
        searchBtn.setLocation(280, 10);
        searchBtn.setSize(80, 30);
        //搜索的点击事件监听
        searchBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取输入框的内容
                String text = searchField.getText();
                //搜索获取所有小说的集合
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ArrayList<Novel> novels = NovelUtils.searchByName(text);
                            System.out.println(novels);
                            bottomPanel.removeAll();//清空之前的书籍的标签
                            initBooksPanel(novels);
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }).start();
            }
        });

        topPanel.add(searchField);// 添加搜索框
        topPanel.add(searchBtn);//添加按钮


        bottomPanel = new JPanel();
        bottomPanel.setPreferredSize(new Dimension(350, 500));
        bottomPanel.setLayout(new FlowLayout());

        //显示所有图书数据的总布局
        scrollPane = new JScrollPane(bottomPanel);
        this.add(topPanel, BorderLayout.NORTH);
        this.add(scrollPane, BorderLayout.CENTER);

    }

    /**
     * 搜索的书籍的布局
     */
    private void initBooksPanel(ArrayList<Novel> novels) {
        int size = novels.size();
        for (int i = 0; i < size; i++) {
            Novel novel = novels.get(i);

            //一本书的布局
            JPanel itemBookPanel = new JPanel();
            itemBookPanel.setPreferredSize(new Dimension(400, 60));
            itemBookPanel.setBackground(Color.cyan);
            itemBookPanel.setLayout(null);
            //添加书的名字标签 书的作者的标签  下载按钮
            JLabel bookNameLabel = new JLabel(novel.getBookName());
            bookNameLabel.setLocation(20, 10);
            bookNameLabel.setSize(300, 15);
            bookNameLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));//设置鼠标样式
            bookNameLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    //点击书名 跳转到一个新的界面 显示所有章节
                    //传入小说对象 和当前页面的对象
                    new ShowChaptersIframe(novel,NovelDownloaderIframe.this);
                    NovelDownloaderIframe.this.setVisible(false);//当前界面隐藏
                }
            });


            JLabel authorNameLable = new JLabel(novel.getAuthorName());
            authorNameLable.setLocation(20, 35);
            authorNameLable.setSize(300, 15);

            JButton downBtn = new JButton("下载");
            downBtn.setLocation(320, 15);
            downBtn.setSize(60, 30);
            downBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                ArrayList<Chapter> chapters = NovelUtils.getChapters(novel.getUrl());
                                for (Chapter chapter : chapters) {
                                    //下载章节
                                    NovelUtils.process_chapter(novel.getBookName(), chapter);
                                }

                            } catch (IOException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    }).start();
                }
            });

            itemBookPanel.add(bookNameLabel);
            itemBookPanel.add(authorNameLable);
            itemBookPanel.add(downBtn);

            //添加单本书布局到总布局
            bottomPanel.add(itemBookPanel);
        }
        bottomPanel.setPreferredSize(new Dimension(350, size * 65));
        bottomPanel.updateUI();//重新更新UI界面
    }
}
